package game.menu;

import game.MainController;
import game.Input.MenuAction;
import game.display.DisplayEngine;
import game.util.HighscoreEntry;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.util.ArrayList;
import java.util.HashSet;

public class Menu 
{
	public HashSet<Drawable> drawables;
	private DisplayEngine de;
	public Menu(DisplayEngine de) 
	{
		drawables = new HashSet<Drawable>();
		this.de = de;
	}

	public static Menu mainMenu, multiplayerMenu,tutorialMenu, highscoreMenu;


	public static void setupMainMenu() 
	{
		mainMenu = new Menu(MainController.displayEngine);

		//Creating the buttons
		double X = .25;
		double Y = .25;
		double W = .5;
		double H = .125;
		double GAP = .0625;

		
		Rectangle r = new Rectangle(0,0,1,150);
		Font font = new Font("Arial", Font.PLAIN, 50);
		mainMenu.addTextField(r,.05, .15, "SPACESHIPRUNSHOOTSTUFF", Color.white, font,true);

		mainMenu.addButton(X, Y, W, H,"SINGLE PLAYER",MenuAction.SINGLE_PLAYER);
		Y = Y + H + GAP;
		mainMenu.addButton(X, Y, W, H,"MULTIPLAYER",MenuAction.MULTIPLAYER);
		Y = Y + H + GAP;
		mainMenu.addButton(X, Y, W, H,"TUTORIAL",MenuAction.TUTORIAL);
		Y = Y + H + GAP;
		mainMenu.addButton(X, Y, W, H,"HIGHSCORE",MenuAction.HIGHSCORE);
	}

	public static void setupMultiplayerMenu() 
	{
		multiplayerMenu = new Menu(MainController.displayEngine);
		Font font = new Font("Arial", Font.PLAIN, 50);

		//Add Multiplayer title
		Rectangle r = new Rectangle(0,0,1,150);
		multiplayerMenu.addTextField(r,.28, .15, "MULTIPLAYER", Color.white, font,true);
		
		//Add Back Button
		double X = .25;
		double Y = .86;
		double W = .5;
		double H = .125;
		
		multiplayerMenu.addButton(.25,.25,W,H,"Host Server",MenuAction.HOSTSERVER);
		multiplayerMenu.addButton(.25,.40,W,H,"Join",MenuAction.JOIN);
		multiplayerMenu.addButton(X, Y, W, H, "BACK", MenuAction.BACK);
	}

	public static void setupHighscore(){
		highscoreMenu = new Menu(MainController.displayEngine);
		Font font = new Font("Arial", Font.PLAIN, 50);
		Rectangle r = new Rectangle(0,0,1,150);
		highscoreMenu.addTextField(r,.32, .15, "HIGHSCORE", Color.white, font,true);
		
		Font fontText = new Font("Arial", Font.PLAIN, 30);
		double offSet = .05;
		ArrayList<HighscoreEntry> entries = MainController.inputEngine.getHighscores();
		for(int i = 0; i < entries.size(); i++){
			HighscoreEntry e = entries.get(i);
			Rectangle r2 = new Rectangle();
			highscoreMenu.addTextField(r2,.25, .25+(offSet*i), e.getName(),Color.white,fontText,false);
			highscoreMenu.addTextField(r2,.65, .25+(offSet*i), ""+e.getScore(),Color.white,fontText,false);
		}
		
		//Add Back Button
		double X = .25;
		double Y = .86;
		double W = .5;
		double H = .125;
						
		highscoreMenu.addButton(X, Y, W, H, "BACK", MenuAction.BACK);
	}

	public static void setupTutorialMenu() 
	{
		tutorialMenu = new Menu(MainController.displayEngine);

		Font fontTitle = new Font("Arial", Font.PLAIN, 50);
		Font fontText = new Font("Arial", Font.PLAIN, 14);

		//Draws the title for the tutorial menu
		Rectangle r = new Rectangle(0,0,1,150);
		tutorialMenu.addTextField(r,.15, .15, "GAME INSTRUCTIONS", Color.white, fontTitle, true);

		//Draws the instructions for the game
		tutorialMenu.addTextField(r,.05, .2, "For movement use the W, A, S, and D keys. Hold A to move left, hold D to move right, hold S to crouch", 
				Color.white, fontText,false);
		tutorialMenu.addTextField(r,.05, .24, "and slide, and press W to jump. To jump higher than normal, jump while crouching.",
				Color.white, fontText,false);

		tutorialMenu.addTextField(r,.05, .34, "To select magic powers, use the number keys (1, 2, 3, 4, and 5) or the scroll wheel on the mouse, only",
				Color.white, fontText,false);
		tutorialMenu.addTextField(r,.05, .38, "after having picked up the appropriate powerup. A display in the top-left corner will tell you which",
				Color.white, fontText,false);
		tutorialMenu.addTextField(r,.05, .42, "colors you have, and the color of the player will tell you which color you will be casting.",
				Color.white, fontText,false);
		
		tutorialMenu.addTextField(r,.05, .52, "For casting magic use the mouse to aim and left or right-click to cast in that direction. ", 
				Color.white, fontText,false);

		tutorialMenu.addTextField(r,.05, .62, "Red magic powers are good against green aliens, green is good against blue, blue is good against red,",
				Color.white, fontText,false);
		tutorialMenu.addTextField(r,.05, .66, "white is neutral but can cast quickly, and yellow is strong, but fires slowly and makes you take more damage.",
				Color.white, fontText,false);
		tutorialMenu.addTextField(r,.05, .70, "White kills give you 100 points, killing an enemy with what they are weak against gives you 150 points,",
				Color.white, fontText,false);
		tutorialMenu.addTextField(r,.05, .74, "Yellow kills give you 400 points, and killing an enemy with what they are resistant against gives you 50 points.",
				Color.white, fontText,false);
		
		tutorialMenu.addTextField(r,.05, .84, "Press esc to exit the game.", Color.white, fontText,false);

		//Creating the buttons
		double X = .25;
		double Y = .86;
		double W = .5;
		double H = .125;
		
		tutorialMenu.addButton(X, Y, W, H, "BACK", MenuAction.BACK);
	}

	public double getHeight() 
	{
		return de.getHeight();
	}

	public double getWidth() 
	{
		return de.getWidth();
	}

	public void drawMenu(Graphics g) 
	{
		int width = MainController.displayEngine.getWidth();
		int height = MainController.displayEngine.getHeight();

		//Sets the background to black
		g.fillRect(0, 0, width, height);

		//Draws the buttons onto the screen
		for(Drawable d: drawables){
			d.draw(g);
		}	
	}
	
	/**
	 * Centers the text from a given rectangle
	 * 
	 * @param s The string you want to paint onto the screen
	 * @param f The current font you are using
	 * @param rectangle The rectangle you want to center the text on
	 * @param g The graphics g
	 */
	private void printTextCenter(String s,Font font, Rectangle rectangle, Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		FontRenderContext frc = g2.getFontRenderContext();
		float sw = (float) font.getStringBounds(s, frc).getWidth();
		LineMetrics lm = font.getLineMetrics(s, frc);
		float sh = lm.getAscent() + lm.getDescent();
		
		// center text in r
		float sx = (float) (rectangle.x + (rectangle.width - sw) / 2);
		float sy = (float) (rectangle.y + (rectangle.height + sh) / 2 - lm.getDescent());
		g2.drawString(s, sx, sy);
	}

	/**
	 * Adds a new button into buttons
	 * @param x The x location of the button
	 * @param y The y location of the button
	 * @param w The width of the button
	 * @param h The height of the button
	 * @param s The string name for the button
	 * @param singlePlayer 
	 */
	public void addButton(double x, double y, double w, double h, String s, MenuAction menuAction)
	{
		drawables.add(new Button(x, y, w, h,s, menuAction, this));
	}

	public void addTextField(Rectangle r,double x, double y, String s, Color c, Font f,boolean title){
		drawables.add(new TextField(r,x, y, s, c, f,this,title));
	}

	public HashSet<Drawable> getDrawables() {
		return drawables;
	}
}
